package ai;

import model.ChessAction;

import java.util.HashMap;

/**
 * 单例模式构造简单模式AI
 *
 * @author 任禹名
 */
public class EasyAI implements Ai {
    private static EasyAI instance;

    private EasyAI(ChessAction chessAction) {
        this.chessAction = chessAction;
        this.nowTable = chessAction.getNowTable();
    }

    public static EasyAI getInstance(ChessAction chessAction) {
        if (instance == null)
            instance = new EasyAI(chessAction);
        return instance;
    }

    private boolean isFirst = true;
    private int[][] siteValue = new int[15][15];
    private ChessNode[][] nowEasyTable = new ChessNode[15][15];
    ChessAction chessAction;
    private int[][] nowTable;
    public HashMap<String, Integer> map = new HashMap<String, Integer>();//设置不同落子情况和相应权值的数组
    private Values values = Values.getInstance();


    public void setFirst(boolean first) {
        isFirst = first;
    }

    @Override
    public void delete(int x, int y) {
        nowEasyTable[x][y] = null;
    }

    @Override
    public int[] downChess(int chessColor) {
        //设置简单棋盘和当前棋盘一致
        for (int i = 0; i < 15; i++) {
            for (int j = 0; j < 15; j++) {
                if (nowTable[i][j] != 0 && nowEasyTable[i][j] == null) {
                    nowEasyTable[i][j] = new ChessNode(i, j, 0);
                    nowEasyTable[i][j].setPlayer(nowTable[i][j]);
                }
            }
        }
        int[] result = new int[2];
        int max = 0;//最大得分
        //如果第一次直接下在中间,或者中间右侧
        if (isFirst) {
            if (nowTable[7][7] == 0) {
                result[0] = 7;
                result[1] = 7;
            } else {
                result[0] = 7;
                result[1] = 8;
            }
            isFirst = false;
        } else {
            getSiteValue(chessColor);//计算每点分数
            for (int i = 0; i < 15; i++) {
                for (int j = 0; j < 15; j++) {
                    if (siteValue[i][j] >= max && nowTable[i][j] == 0) {//找到最大得分点
                        max = siteValue[i][j];
                        result[0] = i;
                        result[1] = j;
                    }
                }
            }
        }
        return result;
    }

    @Override
    public void reset(ChessAction chessAction) {
        this.chessAction = chessAction;
        this.nowTable = chessAction.getNowTable();
        isFirst = true;
        for (int i = 0; i < 15; i++) {
            for (int j = 0; j < 15; j++) {
                siteValue[i][j] = 0;
                nowEasyTable[i][j] = null;
            }
        }
    }

    /**
     * 获取当前所有点的权值.
     *
     * @param player
     */
    public void getSiteValue(int player) {
        for (int i = 0; i < 15; i++) {
            for (int j = 0; j < 15; j++) {
                if (nowTable[i][j] == 0) {
                    ChessNode chessNode = new ChessNode(i, j, player);
                    nowEasyTable[i][j] = chessNode;
                    // 攻击分
                    int offense = values.getValue(chessNode.getX(), chessNode.getY(), player, nowEasyTable);
                    // 防守分
                    int defence = values.getValue(chessNode.getX(), chessNode.getY(), 3 - player, nowEasyTable);
                    siteValue[i][j] = offense + defence;
                    nowEasyTable[i][j] = null;
                }
            }
        }
    }
}
